unit AppContxt;

interface
uses
  Classes,SysUtils,Uni,UniEngine,UniConnct,Class_Dict;

type
  TAppContxt=class(TUniEngine)
  private
    FStrsDict:TStringList;
  public
    procedure UpdateDict;  
  public
    function  GetObjDict(ADictMode:string):TDict;
  public  
    function  CheckCnfg(ADictMode:string):Boolean;
  public
    destructor Destroy; override;
    constructor Create;   
  end;

var
  AppContxtEx:TAppContxt;
    
implementation
uses
  Class_AppUtil,Class_KzUtils;


{ TAppContxt }

function TAppContxt.CheckCnfg(ADictMode: string): Boolean;
var
  DictA:TDict;
begin
  Result:=False;
  DictA:=nil;
  DictA:=GetObjDict(ADictMode);
  if DictA=nil then Exit;
  Result:=DictA.DictCode='1';
end;

constructor TAppContxt.Create;
begin
  FStrsDict:=nil;
end;

destructor TAppContxt.Destroy;
begin
  if FStrsDict<>nil then
  begin
    TKzUtils.TryFreeAndNil(FStrsDict);
  end;  
  inherited;
end;

function TAppContxt.GetObjDict(ADictMode: string): TDict;
var
  IdexA:Integer;
begin
  Result:=nil;

  if FStrsDict=nil then
  begin
    UpdateDict
  end;

  if (FStrsDict=nil) or (FStrsDict.Count=0) then Exit;

  IdexA:=FStrsDict.IndexOf(ADictMode);
  if IdexA=-1 then Exit;

  Result:=TDict(FStrsDict.Objects[IdexA]);
end;

procedure TAppContxt.UpdateDict;
var
  SQLA:string;
  UniConnct:TUniConnection;
begin
  SQLA:='SELECT * FROM TBL_UNIDICT';
  try
    UniConnct:=UniConnctEx.GetConnection('-1');
    //->
    if FStrsDict<>nil then
    begin
      TKzUtils.TryFreeAndNil(FStrsDict);
    end;
    FStrsDict:=TDict.StrsDB(SQLA,UniConnct);
    //-<
  finally
    FreeAndNil(UniConnct);
  end;        
end;

initialization
begin
  AppContxtEx:=TAppContxt.Create;
end;

finalization
begin
  FreeAndNil(AppContxtEx);
end;

end.
